# -*- coding: utf-8 -*-
import importlib
import threading
import zipfile

import requests
import sys

import tqdm
from PyQt6 import QtWidgets
from PyQt6.QtWidgets import QMessageBox
from pyqt_6.ui_py.ui_Main import Ui_MainWindow
from utils.common.dumpUtil import unescape, filter_non_hex_chars

# 空字典用于存储socket
socket_dict = {}
# 初始化一个事件对象,用于通知定时器线程结束
timer_event = threading.Event()


def get_current_version_from_file(default_version="1.1"):
    try:
        with open('v.txt', 'r') as f:
            current_server_version = f.read().strip()  # 读取版本号并去除空白符
            return current_server_version
    except FileNotFoundError:
        print("Error: v.txt 文件未找到。")
        # 如果文件不存在，创建文件并写入默认版本号
        with open('v.txt', 'w') as f:
            f.write(default_version)
        return None  # 处理文件不存在的情况


# 获取版本号的html页面
def get_version_from_url(url):
    try:
        response = requests.get(url)
        response.raise_for_status()  # 检查请求是否成功
        return response.text
    except requests.exceptions.RequestException as e:
        print(f"Error: 无法获取页面 - {e}")
        return None


# 从 HTML 中手动提取版本号
def extract_version_from_html(html):
    # 手动查找包含 class="line" 和 id="LC1" 的 div 标签
    start_tag = "<div class='line' id='LC1'>"
    end_tag = "</div>"

    # 查找起始和结束标签的位置
    start_index = html.find(start_tag)
    if start_index == -1:
        return None

    # 计算内容的起始位置
    start_index += len(start_tag)
    end_index = html.find(end_tag, start_index)

    if end_index == -1:
        return None

    # 提取出标签中的内容
    version = html[start_index:end_index].strip()

    # 替换 HTML 实体字符为对应的符号
    version = version.replace('&#x000A;', '')  # 替换 &#x000A; (换行符) 为空字符

    return version


def show_update_prompt():
    msg_box = QMessageBox()
    msg_box.setIcon(QMessageBox.Icon.Information)
    msg_box.setText("有新版本，是否进行升级？")
    msg_box.setWindowTitle("新版本升级提示")
    msg_box.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
    result = msg_box.exec()
    return result == QMessageBox.StandardButton.Yes


def perform_update(new_version):
    # 执行升级逻辑，例如下载新版本、进度条滚动、替换文件等
    new_version = new_version.strip()
    v_new_version = new_version
    download_url = f"https://gitee.com/vonjia/jt808/releases/download/{new_version}/JT808_{new_version}_Windows_x86_64.zip"
    response = requests.get(download_url, stream=True)

    # 显示下载进度
    total_size = int(response.headers.get('content-length', 0))
    new_version = f"JT808_{new_version}_Windows_x86_64.zip"
    with open(new_version, 'wb') as f, tqdm.tqdm(total=total_size, unit='iB', unit_scale=True, disable=True) as pbar:
        for chunk in response.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
                pbar.update(len(chunk))
    # 解压更新文件
    with zipfile.ZipFile(new_version, 'r') as zip_ref:
        zip_ref.extractall('.')
    with open('v.txt', 'w') as f:
        f.write(v_new_version)


def check_for_update(current_version_inner):
    # 模拟检测新版本的网络请求
    server_temp_version = get_version_from_url("https://gitee.com/vonjia/jt808/blob/master/v.txt")
    server_temp_version = extract_version_from_html(server_temp_version)
    print(server_temp_version)
    if current_version_inner != server_temp_version:
        return True, server_temp_version
    else:
        return False, server_temp_version


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    """
        初始化
    """

    def __init__(self):
        super(MainWindow, self).__init__()
        # super().__init__()  # 直接调用父类的构造函数
        self.setupUi(self)
        self.sock = None  # 用于保存socket对象
        self.show()  # 显示窗口

    def on_combobox_changed(self):
        # 清理先前导入的模块（如果有）
        if hasattr(self, 'module'):
            del self.module
        # 获取当前选择的文本
        selected_text = self.comboBox.currentText()
        # 根据选择的文本调用相应的处理逻辑
        if selected_text == "JT/808-2019":
            # 动态导入2019.py模块
            module = importlib.import_module("packet.jt808_2019.jt808_header")
            print("test")
        elif selected_text == "JT/808-2011":
            module = importlib.import_module("packet.jt808_2011.jt808_header")
        return module

    def parse_print(self):
        module = self.on_combobox_changed()
        print("开始解析")
        parser = module.JT808Parser()
        data = self.dumpText.toPlainText().upper()
        data = filter_non_hex_chars(data)  # 调用规范报文方法
        start_flag = '7E'
        messages = []
        total_messages = 0
        start_index = 0
        result_strings = []  # 使用列表存储所有解析结果
        data = data.upper()
        while True:
            # 在当前起始标志后查找下一个起始标志
            start_index = data.find(start_flag, start_index)
            if start_index == -1:
                break  # 没有找到更多的起始标志
            # 在当前起始标志之后查找下一个起始标志
            end_index = data.find(start_flag, start_index + 1)
            if end_index == -1:
                break  # 没有找到匹配的结束标志
            # 提取报文内容
            message = data[start_index:end_index + len(start_flag)]
            messages.append(message)
            total_messages += 1

            message = unescape(message)  # 调用恢复转义方法
            result_strings.append(
                f'===============================第{total_messages}条包===============================\n' + parser.parse(
                    message))  # 追加解析结果
            # 移动起始索引以继续搜索下一个报文
            start_index = end_index + len(start_flag)
        # 将所有解析结果合并为一个字符串
        final_result = "\n".join(result_strings)
        self.dumpParseText.setPlainText(final_result)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    current_version = get_current_version_from_file()  # 你的当前版本号
    has_update, server_version = check_for_update(current_version)
    if has_update:
        if show_update_prompt():
            perform_update(server_version)
            app.quit()
            window = MainWindow()  # 覆盖原窗口
    sys.exit(app.exec())
